package com.shire.shire_interface.controller.tag;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.group.WxGroup;
import com.shire.shire_interface.model.group.WxGroupExample;
import com.shire.shire_interface.model.tag.WxTag;
import com.shire.shire_interface.model.tag.WxTagExample;
import com.shire.shire_interface.model.tag.WxTagRecord;
import com.shire.shire_interface.model.tag.WxTagRecordExample;
import com.shire.shire_interface.service.group.WxGroupService;
import com.shire.shire_interface.service.tag.WxTagRecordService;
import com.shire.shire_interface.service.tag.WxTagService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


//@Api(tags = "群管理 群标签")
@RestController
@RequestMapping("/tag")
public class WxTagController extends BaseController {

    @Resource
    private WxTagService wxTagService;
    @Resource
    private WxTagRecordService wxTagRecordService;
    @Resource
    private WxGroupService wxGroupService;

//    @ApiOperation(value = "添加设置 csId, wxAccount, groupNoList, tagName")
    @ResponseBody
    @PostMapping("/insertSetting")
    public Response groupTag(@RequestBody JSONObject jsonObject, HttpSession session) {
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            String wxAccount = jsonObject.getString("wxAccount");
            List<String> groupNoList = (List<String>)jsonObject.get("groupNoList");
            String tagName = jsonObject.getString("tagName");

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            WxTag wxTag = new WxTag();
            wxTag.setCsid(csId);
            wxTag.setWxaccunt(wxAccount);
            wxTag.setType(1);
            wxTag.setTagname(tagName);
             wxTagService.insert(wxTag);
            for (String groupNo : groupNoList) {
                WxTagRecord wxTagRecord = new WxTagRecord();
                wxTagRecord.setTagid(wxTag.getId());
                wxTagRecord.setWxacount(wxAccount);
                wxTagRecord.setFriendGroupId(groupNo);
                wxTagRecordService.insert(wxTagRecord);
            }


            Integer tagId = wxTag.getId();
            WxTagRecordExample wxTagRecordExample = new WxTagRecordExample();
            wxTagRecordExample.createCriteria().andWxacountEqualTo(wxAccount).andTagidEqualTo(tagId);
            List<WxTagRecord> wxTagRecords = wxTagRecordService.selectByExample(wxTagRecordExample);

            WxGroupExample wxGroupExample = new WxGroupExample();

            List<WxGroup> wxGroupList = new ArrayList<>();
            for (WxTagRecord wxTagRecord : wxTagRecords) {
                wxGroupExample.clear();
                String friendGroupId = wxTagRecord.getFriendGroupId();
                wxGroupExample.createCriteria().andWxaccountEqualTo(wxAccount).andGroupnoEqualTo(friendGroupId);
                List<WxGroup> wxGroups = wxGroupService.selectByExample(wxGroupExample);
                if (wxGroups.size()>0){
                    WxGroup wxGroup = wxGroups.get(0);
                    wxGroupList.add(wxGroup);
                }
            }
            wxTag.setGroupList(wxGroupList);
            response.setData(wxTag);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "查询设置 csId， wxAccount")
    @ResponseBody
    @PostMapping("/getSetting")
    public Response getSetting(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            String wxAccount = jsonObject.getString("wxAccount");
            WxTagExample wxTagExample = new WxTagExample();
            wxTagExample.createCriteria().andCsidEqualTo(csId).andWxaccuntEqualTo(wxAccount);
            List<WxTag> wxTags = wxTagService.selectByExample(wxTagExample);
            response.setData(wxTags);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }
}
